import { sortBy } from 'lodash-es';
import { ComponentTypeEnum } from '/@/components/ModelTable/src/types';

export function useEchoTableConfig(data: Indexable, setFieldsValue: Fn) {
  const { tableButtons, titles, defSort, tableSetting = {} } = data;

  setFieldsValue({
    ...data,
    ...tableSetting,
  });

  return {
    tableButtons: tableButtons || [],
    titles: sortBy(titles || [], (a: Indexable) => [0, 3].includes(a.displayType)),
    defSort: defSort || [],
  };
}

export function useEchoFormConfig(data: Indexable, setFieldsValue: Fn) {
  const { btns: tableButtons, schemas, event } = data;
  setFieldsValue(data);
  return {
    event: event || {},
    tableButtons: tableButtons || [],
    schemas: sortBy(schemas || [], (a: Indexable) => a.componentType === ComponentTypeEnum.Hide),
  };
}
